<?php

namespace wx;

use app\common\model\Users;
use think\Db;

class Wechat
{
    protected $data = array('flag' => 0, 'msg' => '', 'data' => '');
//    protected $appid = '';
//    protected $redirect_uri = '';
//    protected $state = '';//mt_rand(100,999);
//    protected $appsecret = '';
//
//    public function __construct()
//    {
//        $this->redirect_uri = '';
//        $this->appid = config('app.appid');//
//        $this->appsecret = config('app.appsecret');//;
//        $this->state = mt_rand(100, 999);
//    }

//    //引导用户访问链接处理函数
//    public function begin($redirect_uri)
//    {
//        $this->redirect_uri = urlencode($redirect_uri);//微信自动跳转到$redirect_uri
//        $ser = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->appid . '&redirect_uri=' . $this->redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=' . $this->state . '&connect_redirect=1#wechat_redirect';
//        header('Location:' . $ser);
//        exit();
//    }

    public function getUser_info($code = '',$snsapi_userinfo=true)
    {
        if (empty($code)) {
            $this->data['flag'] = 0;
            $this->data['msg'] = '授权失败！';
        } else {
            // 获取页面URL的CODE参数，判断是否有值
            // 获取openid和access_token
            $app_id = config('app.appid');
            $app_secret = config('app.appsecret');
            // 发送请求，获取用户openid和access_token
            $data = $this->get_by_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $app_id . '&secret=' . $app_secret . '&code=' . $code . '&grant_type=authorization_code');
            $data = json_decode($data);

            // 防止第二次访问动态链接报错
            // 判断是否获取到当前用户的openid
            if (isset($data->openid)) {
                $open_id = $data->openid;
                if ($snsapi_userinfo){//判断是否是非静默授权
                    $access_token = $data->access_token;
                    // 获取当前用户信息
                    $user_info = $this->get_by_curl('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $open_id . '&lang=zh_CN');
                    $user_info = json_decode($user_info);
                    $this->data['data']=$user_info;
                    $this->data['flag']=1;
                }
                $this->data['data']=['openid'=>$open_id];
                $this->data['flag']=1;
                $this->data['msg']='获取成功';
                return  $this->data;
                // 取出用户信息
                /*
                *user_openid :用户openId
                *user_nickname :用户昵称
                *user_sex :性别
                *user_province :省
                *user_city :城市
                *user_headimgurl :用户头像url
                */
//                $user_openid = $user_info->openid;
//                $user_nickname = $user_info->nickname;
//                $user_sex = $user_info->sex;
//                $user_province = $user_info->province;
//                $user_city = $user_info->city;
//                $user_headimgurl = $user_info->headimgurl;
            }

            $this->data['msg']='获取信息失败';
            return $this->data;
        }
    }


    // 用于请求微信接口获取数据
    function get_by_curl($url, $post = false)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

}